﻿using System;
using System.Diagnostics;
using Hallenberg.SmartClient.Interfaces;
using System.Reflection;

namespace Hallenberg.SmartClient.TypeHandlers
{
    public abstract class BaseAttributeTypeHandler<T> : IAttributeTypeHandler
        where T : class
    {
        public Type HandledType
        {
            get { return typeof(T); }
        }

        public bool Handle(Type attachedToClass, MethodBase attachedToMethod, Attribute attributeInstance, Attribute[] allAttributes)
        {
            Debug.Assert(attachedToClass != null);
            if (attachedToClass == null)
            {
                throw new ArgumentNullException("attachedToClass");
            }

            Debug.Assert(attachedToMethod != null);
            if (attachedToMethod == null)
            {
                throw new ArgumentNullException("attachedToMethod");
            }

            Debug.Assert(attributeInstance != null);
            if (attributeInstance == null)
            {
                throw new ArgumentNullException("attributeInstance");
            }

            T typedAttribute = attributeInstance as T;

            Debug.Assert(typedAttribute != null);
            if (typedAttribute == null)
            {
                throw new InvalidOperationException("Incorrect instance type");
            }

            return Handle(attachedToClass, attachedToMethod, typedAttribute, allAttributes);
        }

        public abstract bool Handle(Type attachedToClass, MethodBase attachedToMethod, T typedAttribute, Attribute[] allAttributes);
    }
}
